package cn.lingyangwl.agile.msg.push.config;

import cn.lingyangwl.agile.msg.push.api.model.mq.*;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 延时消息队列
 * @author shenguangyang
 */
@Configuration
public class MsgDelayedMqConfig {

    // ============================================================= 推送延时队列绑定配置
    /**
     * 自定义交换机 我们在这里定义的是一个延迟交换机
     * 需要在rabbitmq上安装延时插件
     * 在官网上下载 <a href="https://www.rabbitmq.com/community-plugins.html">官网</a>，下载
     * rabbitmq_delayed_message_exchange 插件
     */
    @Bean
    public CustomExchange pushDelayExchange() {
        Map<String, Object> args = new HashMap<>();
        // 自定义交换机的类型
        args.put("x-delayed-type", "direct");
        return new CustomExchange(MsgPushMQ.DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }

    @Bean
    public Queue pushDelayQueue() {
        Map<String, Object> arguments = new HashMap<>();
        return new Queue(MsgPushMQ.DELAYED_QUEUE, true, false, false, arguments);
    }

    @Bean
    public Binding pushDelayBinding(@Qualifier("pushDelayQueue") Queue queue, @Qualifier("pushDelayExchange") CustomExchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("").noargs();
    }
}
